﻿using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project362.Files;
using Project362.Tags;

namespace Project362.Collections
{
    [Serializable()]
    public class Collection : ISerializable
    {
        private Dictionary<string, FileClass> dir;
        private string name;
        public string Name { get { return name; } }
        /// <summary>
        /// Initializes a new collection
        /// </summary>
        public Collection(string name)
        {
            dir = new Dictionary<string, FileClass>();
            this.name = name;
        }
        public Collection(SerializationInfo info, StreamingContext stream){
            this.name=(string)info.GetValue("Name",typeof(string));
            this.dir=(Dictionary<string,FileClass>)info.GetValue("Dictionary",typeof(Dictionary<string,FileClass>));
        }
        /// <summary>
        /// Adds a file to this collection
        /// </summary>
        /// <param name="filename">Location of file to add</param>
        /// <returns>success or failure</returns>
        public bool addFile(string filename, string name)
        {
            FileClass f = new FileClass(filename, name, this.name);
            dir.Add(name, f);
            return true;
        }
        /// <summary>
        /// Adds a file to this collection
        /// </summary>
        /// <param name="file">FileClass object (already constructed)</param>
        /// <returns>Success or failure</returns>
        [Obsolete()]
        public bool addFile(FileClass file)
        {
            dir.Add(file.Name, file);
            return true;
        }
        /// <summary>
        /// Removes a file from this collection
        /// </summary>
        /// <param name="filename">location of file to remove</param>
        /// <returns>success or failure</returns>
        public bool removeFile(string name)
        {
            if (!dir.ContainsKey(name)) { return false; }
            else
            {
                dir.Remove(name);
                return true;
            }
        }
        /// <summary>
        /// Removes a file from this collection
        /// </summary>
        /// <param name="file">FileClass object to remove</param>
        /// <returns>success or failure</returns>
        [Obsolete()]
        public bool removeFile(FileClass file)
        {
            if (!dir.ContainsKey(file.Name)) { return false; }
            else
            {
                foreach (Tag t in file.getTags())
                {
                    t.removeFile(file);
                }
                dir.Remove(file.Name);
                return true;
            }
        }
        /// <summary>
        /// get files in this collection
        /// </summary>
        /// <returns>a list of all fileclass objects this collection stores</returns>
        public List<FileClass> getFiles()
        {
            List<FileClass> l = new List<FileClass>();
            foreach (FileClass f in dir.Values)
            {
                l.Add(f);
            }
            return l;
        }
        /// <summary>
        /// Check if this collection contains a particular file
        /// </summary>
        /// <param name="filename">File to check</param>
        /// <returns>Yes or no</returns>
        public bool contains(string name)
        {
            return dir.ContainsKey(name);
        }
        /// <summary>
        /// Get a fileclass object from this collection
        /// </summary>
        /// <param name="filename">location of object to get</param>
        /// <returns>FileClass object or null</returns>
        public FileClass getFile(string name)
        {
            if (!dir.ContainsKey(name)) { return null; }
            else
            {
                return dir[name];
            }
        }
        public bool addAttributetoFile(string file, string metaname, string contents)
        {
            return dir[file].addAttribute(metaname, contents);
        }
        public List<FileClass> getFilesWithMeta(string key, string value)
        {
            List<FileClass> ret = new List<FileClass>();

            foreach (FileClass f in dir.Values)
            {
                string v = f.getMetadataContents(key);
                if (v != null && v.Equals(value))
                    ret.Add(f);
            }

            return ret;
        }
        public void GetObjectData(SerializationInfo info, StreamingContext stream)
        {
            info.AddValue("Dictionary", dir);
            info.AddValue("Name", name);
        }
    }
}
